﻿namespace AnyOffice.general.ipanel
{
    using AjaxPro;
    using AnyOffice.Components;
    using AnyOffice.DAL;
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Web;
    using System.Reflection;

    public class AjaxMethods
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        [AjaxMethod]
        public void ChangeState(string state)
        {
            try
            {
                int num = Convert.ToInt32(state);
                SqlConnection connection = new SqlConnection(ConfigManager.ConnectionString);
                new UsersDao().ChangeState(connection, HttpContext.Current.User.Identity.Name, num);
                if (connection.State == ConnectionState.Open) connection.Close();
                UserCache.Update(HttpContext.Current.User.Identity.Name);
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) log.Error("", exception);
            }
        }

        private void GetDept(SqlConnection conn, int deptId, ref string deptName)
        {
            string str = "SELECT [ParentDept],[DeptName] FROM [dbo].[Department] WHERE [DeptId] = " + deptId.ToString();
            DataTable table = SqlHelper.ExecuteDataset(conn, CommandType.Text, str).Tables[0];
            if (table.Rows.Count > 0)
            {
                deptName = table.Rows[0]["DeptName"].ToString() + "/" + deptName;
                if (Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()) != 0) this.GetDept(conn, Convert.ToInt32(table.Rows[0]["ParentDept"].ToString()), ref deptName);
            }
        }

        private string GetDeptName(SqlConnection conn, int parentDept, string deptName)
        {
            if (parentDept != 0) this.GetDept(conn, parentDept, ref deptName);
            return deptName;
        }

        private string GetOnlineStatus(int state)
        {
            switch (state)
            {
                case 1:
                    return "联机";

                case 2:
                    return "忙碌";

                case 3:
                    return "离开";
            }
            return "联机";
        }

        [AjaxMethod]
        public string GetOnlineUsers()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                int num = 0;
                SqlConnection conn = new SqlConnection(ConfigManager.ConnectionString);
                DataTable table = new UsersDao().SelectOnline(conn, HttpContext.Current.User.Identity.Name);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if (num == 0)
                        {
                            builder.Append("<table class=TableBlock width=100% class=small>");
                            builder.Append("<tr class=TableHeader onclick='list_expand()' style=cursor:hand>");
                            builder.Append("<td align=center id='expand_text'>全部收缩</td>");
                            builder.Append("</tr>");
                            builder.Append("<tr class=TableHeader onclick=clickMenu('trdept_" + table.Rows[i]["DeptId"].ToString() + "') style=cursor:hand>");
                            builder.Append("<td align=center>" + this.GetDeptName(conn, Convert.ToInt32(table.Rows[i]["ParentDept"].ToString()), table.Rows[i]["DeptName"].ToString()) + "</td>");
                            builder.Append("</tr>");
                            builder.Append("</table>");
                            builder.Append("<table class=TableBlock width=100% id='trdept_" + table.Rows[i]["DeptId"].ToString() + "' class=small>");
                            builder.Append("<tr class=TableData align=center>");
                            builder.Append("<td width=40>&nbsp;");
                            builder.Append("<img title=" + this.GetOnlineStatus(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                            builder.Append("</td>");
                            builder.Append("<td align=center><a href=javascript:openURL('/general/ipanel/user_info.aspx?user_id=" + table.Rows[i]["UserId"].ToString() + "')>" + table.Rows[i]["UserName"].ToString() + "</a></td>");
                            builder.Append("<td width=60><a href=javascript:send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                            builder.Append("</tr>");
                        }
                        else if (num != Convert.ToInt32(table.Rows[i]["DeptId"].ToString()))
                        {
                            builder.Append("</table>");
                            builder.Append("<table class=TableBlock width=100% class=small>");
                            builder.Append("<tr class=TableHeader onclick=clickMenu('trdept_" + table.Rows[i]["DeptId"].ToString() + "') style=cursor:hand>");
                            builder.Append("<td align=center>" + this.GetDeptName(conn, Convert.ToInt32(table.Rows[i]["ParentDept"].ToString()), table.Rows[i]["DeptName"].ToString()) + "</td>");
                            builder.Append("</tr>");
                            builder.Append("</table>");
                            builder.Append("<table class=TableBlock width=100% id='trdept_" + table.Rows[i]["DeptId"].ToString() + "' class=small>");
                            builder.Append("<tr class=TableData align=center>");
                            builder.Append("<td width=40>&nbsp;");
                            builder.Append("<img title=" + this.GetOnlineStatus(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                            builder.Append("</td>");
                            builder.Append("<td align=center><a href=javascript:parent.openURL('/general/ipanel/user_info.aspx?user_id=" + table.Rows[i]["UserId"].ToString() + "')>" + table.Rows[i]["UserName"].ToString() + "</a></td>");
                            builder.Append("<td width=60><a href=javascript:send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                            builder.Append("</tr>");
                        }
                        else
                        {
                            builder.Append("<tr class=TableData align=center>");
                            builder.Append("<td width=40>&nbsp;");
                            builder.Append("<img title=" + this.GetOnlineStatus(Convert.ToInt32(table.Rows[i]["State"].ToString())) + " src=/images/on_status" + table.Rows[i]["State"].ToString() + ".gif align=absmiddle>&nbsp;");
                            builder.Append("</td>");
                            builder.Append("<td align=center><a href=javascript:openURL('/general/ipanel/user_info.aspx?user_id=" + table.Rows[i]["UserId"].ToString() + "')>" + table.Rows[i]["UserName"].ToString() + "</a></td>");
                            builder.Append("<td width=60><a href=javascript:send_sms('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>消息</a>&nbsp;<a href=javascript:send_email('" + table.Rows[i]["UserId"].ToString() + "','" + table.Rows[i]["UserName"].ToString() + "')>邮件</a></td>");
                            builder.Append("</tr>");
                        }
                        num = Convert.ToInt32(table.Rows[i]["DeptId"].ToString());
                    }
                    builder.Append("</table>");
                }
                if (conn.State == ConnectionState.Open) conn.Close();
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) log.Error("", exception);
            }
            return builder.ToString();
        }
    }
}

